package com.sunny.hp.common.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Functions {
	public static String no(Integer totalRows, Integer pagePerRows, Integer currentPageRowOffset, Integer currentPage) {
		return String.valueOf(totalRows - (((pagePerRows * currentPage) - pagePerRows) + currentPageRowOffset));
	}
	
	public static String numberFormat(Object obj) {
		try {
			java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
			return nf.format(obj);
		} catch (Exception e) {
			return "0";
		}
	}
	
	public static String dateFormat(Date date, String format) {
		try {
			if(date == null) return "";
			
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			
			return sdf.format(date);
		} catch (Exception e) {
			return "";
		}
	}
	
	public static String md5(String input) {
		try {
	        MessageDigest md = MessageDigest.getInstance("MD5");
	        md.update(input.getBytes());
	        byte[] md5 = md.digest();

		    StringBuffer buf = new StringBuffer();
	        for (int i = 0; i < md5.length; i++) { 
	            int halfbyte = (md5[i] >>> 4) & 0x0F;
	            int two_halfs = 0;
	            do { 
	                if ((0 <= halfbyte) && (halfbyte <= 9)) 
	                    buf.append((char) ('0' + halfbyte));
	                else 
	                    buf.append((char) ('a' + (halfbyte - 10)));
	                halfbyte = md5[i] & 0x0F;
	            } while(two_halfs++ < 1);
	        } 
		    return buf.toString();
		} catch (Throwable e) {
			return null;
		}
	}
	
	public static String sha1(String input) {
		try {
			MessageDigest md;
		    md = MessageDigest.getInstance("SHA-1");
		    byte[] sha1hash = new byte[40];
		    md.update(input.getBytes("iso-8859-1"), 0, input.length());
		    sha1hash = md.digest();
		    
		    StringBuffer buf = new StringBuffer();
	        for (int i = 0; i < sha1hash.length; i++) { 
	            int halfbyte = (sha1hash[i] >>> 4) & 0x0F;
	            int two_halfs = 0;
	            do { 
	                if ((0 <= halfbyte) && (halfbyte <= 9)) 
	                    buf.append((char) ('0' + halfbyte));
	                else 
	                    buf.append((char) ('a' + (halfbyte - 10)));
	                halfbyte = sha1hash[i] & 0x0F;
	            } while(two_halfs++ < 1);
	        } 
	        return buf.toString();
		} catch (Throwable e) {
			return null;
		}
	}
	
	public static boolean empty(String str) {
		if(str == null) return true;
		if("".equals(str)) return true;
		
		return false;
	}
}
